<template>
  <div class="app-container">

    

    <el-button type="primary" @click="onEdit" v-if="!isEdit">编辑</el-button>
    <el-button type="primary" @click="onSave" v-else>保存</el-button>

    <el-card class="box-card mt">
      <!--表格渲染-->
      <el-table 
      v-loading="listLoading" 
      :data="list"
      border 
      fit 
      highlight-current-row
        style="width: 100%; margin-top: 10px" 
        :tree-props="{ children: 'child' }" 
        row-key="id"
      >
  
        <el-table-column :show-overflow-tooltip="true" prop="name" label="名称">
          <template #default="scope">
            <span v-if="isEdit" >
            <el-input v-model="scope.row.name" placeholder="Please input" style="width: 20rem;" />
            </span>
            <span v-else>
              {{ scope.row.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="type" label="类型" width="200">
          <template #default="scope">
            <el-select v-model="scope.row.type" class="m-2" placeholder="Select" v-if="isEdit">
              <el-option v-for="item in categoryOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>

            <el-tag type="success" v-else>{{ getType(scope.row.type) }}</el-tag>

          </template>
        </el-table-column>   

        <el-table-column :show-overflow-tooltip="true" prop="seq" label="排序" width="80" >
        <template #default="scope">
          <el-input v-model="scope.row.seq" placeholder="Please input" v-if="isEdit" />
          <span v-else>
            {{ scope.row.seq }}
          </span>
        </template>
</el-table-column>
      </el-table>

    </el-card>
  </div>
</template>

<script>
import { getAllCategory, saveCategory } from "@/api/zhongyi/examination";


export default {
  name: "yaocaitag",
  data() {
    return {
      list: null,
      listLoading: true,
      isEdit: false,
      categoryOptions: [{
        value: 1,
        label: '职称'
      }, {
        value: 2,
        label: '试卷分类'
      }, {
        value: 3,
        label: '学科'
      }
      ]
    };
  },
  computed: {
    getType() {
      var _this = this
      return function (val) {
        return _this.categoryOptions.find(t => t.value == val).label;
      };
    },
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      getAllCategory().then((res) => {
        this.list = res.result;
        // this.total = res.result.total;
        this.listLoading = false;
      });
    },

    onEdit() {
      this.isEdit = true;
    },
    onSave() {
      this.listLoading = true;
      saveCategory(this.list).then(_=> {
        this.isEdit = false;
        this.listLoading = false;
      })
      
    },

    onSearch() {
      this.getList();
    },
  },
  mounted: function () { },
  watch: {
    query: {
      handler(val, oldVal) {
        console.log(this.query)
        //this.getList();
      },
      deep: true
    },
  }
};
</script>


